package chapter4_hash;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Author yuh
 * @Date Created in 上午7:57 2018/4/10
 * @Description 要灵活运用自带的类库的表现形式
 */
public class EqualDistance {


    static int eqDistance(int[][] points) {
        int all = 0;
        for (int i = 0; i < points.length; i++) {
            HashMap<Integer, Integer> map = new HashMap<>();
            for (int j = 0; j < points.length; j++) {
                if (i != j) {
                    int dis = dis(points, i, j);
                    if (map.containsKey(dis)) {
                        map.put(dis, map.get(dis) + 1);
                    } else {
                        map.put(dis, 1);
                    }
                }
            }

            Set<Map.Entry<Integer, Integer>> entrySet = map.entrySet();
            for (Map.Entry<Integer, Integer> entry : entrySet) {
                all += entry.getValue() * (entry.getValue() - 1);
            }
        }

        return all;
    }

    static int dis(int[][] points, int i, int j) {
        int[] point1 = points[i];
        int[] point2 = points[j];
        return (point1[0] - point2[0]) * (point1[0] - point2[0]) +
                (point1[1] - point2[1]) * (point1[1] - point2[1]);
    }


    public static void main(String[] args) {
        int[][] points = {{0, 0}, {1, 0}, {2, 0}};
        System.out.println(eqDistance(points));
    }
}
